﻿using Microsoft.Win32;
using System.Xml;
using System;

namespace Sinacor.OMM.Util
{
    public class WindowsRegistry
    {
        #region [ Objects ]

        private XmlDocument _documentParameter;

        #endregion

        #region [ Constructor ]

        public WindowsRegistry()
        {
            _documentParameter = new XmlDocument();

        }

        #endregion

        #region [ Public Methods ]

        public static bool KeyExists(string key, RegistryKey baseGroupKey)
        {
            RegistryKey registryKey = null;
            bool keyExists = false;

            try
            {
                registryKey = baseGroupKey.OpenSubKey(key);
                keyExists = (registryKey != null);
            }
            catch
            {
                keyExists = false;
            }
            finally
            {
                if (registryKey != null)
                    registryKey.Close();
            }

            return keyExists;
        }

        public static void CreateRegistry(string keyName, string valueName, string value)
        {
            try
            {
                Microsoft.Win32.Registry.SetValue(keyName, valueName, value); // key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(registryKey.Name);                                        
            }
            catch
            {
                throw;
            }
        }

        public static string GetRegistryValue(string keyName, string valueName)
        {
            if (!String.IsNullOrEmpty(Convert.ToString(Registry.GetValue(keyName, valueName, ""))))
                return Convert.ToString(Registry.GetValue(keyName, valueName, ""));

            return String.Empty;
        }

        #endregion
    }
}
